/**
 * 
 */
package com.luckmgr.web;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.common.constant.SystemConstant;
import com.common.entity.PageBean;
import com.common.utils.StringUtils;
import com.luckmgr.entity.db.Activity;
import com.luckmgr.entity.db.ActivityLevel;
import com.luckmgr.entity.db.LevelPrize;
import com.luckmgr.entity.db.Prize;
import com.luckmgr.entity.db.User;
import com.luckmgr.service.ActivityLevelService;
import com.luckmgr.service.ActivityService;
import com.luckmgr.service.LevelPrizeService;
import com.luckmgr.service.PrizeService;
import com.luckmgr.util.ExcelUtil;

/**
 * 
 * @author duanhx
 * @version [Ver 1.0; 2012-7-22]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
@Controller
@RequestMapping("/leprize")
public class LevelPrizeController {
	@Autowired
	PrizeService prizeServiceImpl;
	@Autowired
	ActivityLevelService activityLevelServiceImpl;
	@Autowired
	ActivityService activityServiceImpl;
	
	@Autowired
	LevelPrizeService levelPrizeServiceImpl;
	
	@RequestMapping(value = "")
	public String main(Model model,HttpSession session,Integer aid,Integer lid,String name,Integer page) {
		if(null == page)
			page = 1;
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("aid", aid);
		map.put("lid", lid);
		PageBean<LevelPrize> pagebean = levelPrizeServiceImpl.selectListPage(map, page);
		List<Activity> activitys = activityServiceImpl.selectList(null);
		
		if(aid!=null){
			List<ActivityLevel> alevel = activityLevelServiceImpl.findLevelByAid(aid);
			model.addAttribute("alevel", alevel);
		}
		
		model.addAttribute("pagebean", pagebean);
		model.addAttribute("activitys", activitys);
		model.addAttribute("aid", aid);
		model.addAttribute("lid", lid);
		return "leprize/index";
	}
	
	@RequestMapping(value = "addup")
	public String addup(Model model,Integer lid,Integer id) {
		LevelPrize levelPrize = null;
		if(null !=id){
			levelPrize = levelPrizeServiceImpl.selectByPrimaryKey(id);
			lid = levelPrize.getLevelId();
			model.addAttribute("entity", levelPrize);
		}
		if(null == lid){
			model.addAttribute("msg", "该数据不正常！");
			return "leprize/addup";
		}
		ActivityLevel alevel = activityLevelServiceImpl.selectByPrimaryKey(lid);
		if(null == alevel){
			model.addAttribute("msg", "奖品组不存在！");
			return "leprize/addup";
		}
		
		Activity activity = activityServiceImpl.selectByPrimaryKey(alevel.getActivityId());
		if(null == activity || activity.getStatus()==1){
			model.addAttribute("msg", "活动已开始或已删除，不能进行此操作!");
			return "leprize/addup";
		}
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("aid", activity.getId());
		List<Prize> prizes = prizeServiceImpl.selectList(map);
		
		model.addAttribute("alevel", alevel);
		model.addAttribute("prizes", prizes);
		
		
		return "leprize/addup";
	}
	
	
	

	
	
	@RequestMapping(value = "update")
	public String update(Model model,HttpSession session,@ModelAttribute("entity") LevelPrize levelPrize) {
		User user = (User) session.getAttribute(SystemConstant.ESSIONUSER);
		if(null == levelPrize.getLevelId()){
			model.addAttribute("msg", "数据不完整请重新录入！");
			return "prize/addup";
		}
		
		levelPrize.setOperatorName(user.getUserName());
		levelPrize.setOperatorIp(user.getLoginIp());
		
		if(levelPrize.getId()!=null && 0!=levelPrize.getId()){
			levelPrize.setPrizeOrig(levelPrize.getPrizeNumber());
			levelPrizeServiceImpl.updateEntity(levelPrize);
		}else{
			if(null == levelPrize.getPrizeId()){
				model.addAttribute("msg", "数据不完整请重新录入！");
				return "prize/addup";
			}
			
			ActivityLevel alevel = activityLevelServiceImpl.selectByPrimaryKey(levelPrize.getLevelId());
			Prize prize = prizeServiceImpl.selectByPrimaryKey(levelPrize.getPrizeId());
			
			if(null == alevel || null == prize){
				model.addAttribute("msg", "活动组或者是奖品不存在！");
				return "leprize/addup";
			}
			Activity activity = activityServiceImpl.selectByPrimaryKey(alevel.getActivityId());
			if(null == activity || activity.getStatus()==1){
				model.addAttribute("msg", "活动已开始或已删除，不能进行此操作!");
				return "leprize/addup";
			}
			
			Map<String,Object> map = new HashMap<String, Object>();
			map.put("lid", alevel.getId());
			map.put("pid", prize.getId());
			
			LevelPrize temlevelPrize  = levelPrizeServiceImpl.selectByMap(map);
			if(null != temlevelPrize){
				model.addAttribute("msg", "该组已经有奖品："+prize.getPrizeName());
				return "leprize/addup";
			}
			
			levelPrize.setActivityId(activity.getId());
			levelPrize.setActivityName(activity.getName());
			levelPrize.setLevelId(alevel.getId());
			levelPrize.setLevelName(alevel.getLevelName());
			levelPrize.setPrizeId(prize.getId());
			levelPrize.setPrizeName(prize.getPrizeName());
			levelPrize.setPrizeOrig(levelPrize.getPrizeNumber());
			levelPrizeServiceImpl.addEntity(levelPrize);
		}
		
		model.addAttribute("msg", "添加成功！");
		return "leprize/addup";
		
	}
	
	



	
	@RequestMapping(value = "delete")
	public String comdelete(Model model,@RequestParam(required = false)Integer id) {
		if(null != id && 0 != id){
			LevelPrize levelPrize = levelPrizeServiceImpl.selectByPrimaryKey(id);
			if(null == levelPrize){
				model.addAttribute("msg", "这个组内奖品已不存在");
				return "leprize/addup";
			}
			Activity activity = activityServiceImpl.selectByPrimaryKey(levelPrize.getActivityId());
			if(null != activity && activity.getStatus()==1){
				model.addAttribute("msg", "活动已开始，不能进行此操作!");
				return "leprize/addup";
			}
			
			levelPrizeServiceImpl.deleteByPrimaryKey(id);
		}
		return "redirect:/leprize";
	}

}
